// 用户模块路由处理函数

// 引入jwt
const jwt = require('jsonwebtoken')
const secretKey = 'ethink'    // 密钥
const utils = require("../../common/utils")
const server = require("../../server/admin/user")

// 获取用户信息
exports.getUserInfo = (req, res) => {
  let token = req.get('Authorization').split(' ')[1]
  // token解析
  const data = jwt.verify(token, secretKey)
  data.register_time = utils.formatDate(new Date(data.register_time).getTime())
  // 获取菜单
  server.getMenuList(data.role_id).then(result => {
    return res.send({
      code: 200,
      message: "获取用户信息成功",
      data: {
        userInfo: data,
        menuList: result
      }
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 修改密码
exports.updatePassword = (req, res) => {
  let data = req.body
  let token = req.get('Authorization').split(' ')[1]
  // token解析
  const userInfo = jwt.verify(token, secretKey)
  server.updatePassword({...data, ...userInfo}).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}